﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EmoneyWeiXinEntity;

namespace EmoneyWeiXinCommon
{
    public class SendModelMessage
    {
        static MemcachedTools1 mmtools = new MemcachedTools1();
        static string url = Config.WeiXin_ApiUrl + "message/template/send?access_token={0}";
        public static bool Send(string accessToken, ModelMessagesEntity item, out bool isneedRefreshToken)
        {
            string json = MappingJson(item.template_id, item);
            string returnJson = TokenHelper.RequestPost(string.Format(url, accessToken), json);
            if (returnJson.IndexOf("{\"errcode\":40001") != -1)
            {
                isneedRefreshToken = true;
                return false;
            }
            else
            {
                isneedRefreshToken = false;
            }
            returnJsonModel jm = JsonTools.ToObject<returnJsonModel>(returnJson);
            if (jm.errmsg == "ok") //
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static string MappingJson(string template_id, ModelMessagesEntity entity)
        {
            if (template_id == Config.YuYuemodelId)
            {
                return CreateYuyueJson(entity);
            }
            if (template_id == Config.KaitongmodelId)
            {
                return CreateKaiTongJson(entity);
            }
            return string.Empty;
        }

        /// 创建预约模板消息
        /// </summary>
        /// <returns></returns>
        public static string CreateYuyueJson(ModelMessagesEntity entity)
        {
            var jsonObj = new
            {
                touser = entity.touser,
                template_id = Config.YuYuemodelId,
                url = entity.url,
                topcolor = "#FF0000",
                data = new
                {
                    first = new { value = entity.firstText, color = "#173177" },
                    keyword1 = new { value = entity.keyNote1, color = "#173177" },
                    keyword2 = new { value = entity.keyNote2, color = "#173177" },
                    remark = new { value = entity.remarkText, color = "#173177" },
                }
            };
            return JsonTools.ToJson(jsonObj);
        }

        public static string CreateKaiTongJson(ModelMessagesEntity entity)
        {
            var jsonObj = new
            {
                touser = entity.touser,
                template_id = Config.KaitongmodelId,
                url = entity.url,
                topcolor = "#FF0000",
                data = new
                {
                    first = new { value = entity.firstText, color = "#173177" },
                    name = new { value = entity.keyNote1, color = "#173177" },
                    time = new { value = entity.keyNote2, color = "#173177" },
                    remark = new { value = entity.remarkText, color = "#173177" },
                }
            };
            return JsonTools.ToJson(jsonObj);

        }



        //public static bool Send()
        //{

        //}


        class returnJsonModel
        {
            public string errcode { get; set; }
            public string errmsg { get; set; }
            public string msgid { get; set; }

        }
    }

}
